package com.hss.recyclerviewtest.itemtouch;

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerViewItemTouchListener  extends RecyclerView.SimpleOnItemTouchListener{

    private GestureDetectorCompat mGestureDetector;
    private  OnItemClickListener mClickListener;

    public RecyclerViewItemTouchListener(final RecyclerView recyclerView, final OnItemClickListener mClickListener) {
        this.mClickListener = mClickListener;
        mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(),new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent e) {

                View child =  recyclerView.findChildViewUnder(e.getX(),e.getY());
                if (child != null && mClickListener !=null){
                    mClickListener.onItemClick(child);
                }
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child =  recyclerView.findChildViewUnder(e.getX(),e.getY());

                if (child != null && mClickListener != null) {
                    mClickListener.onItemLongClick(child);
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return mGestureDetector.onTouchEvent(e);
    }
}
